package com.yixin.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.entity.domain.DrugType;
import com.yixin.hospital.entity.dto.req.drugType.DrugTypeAddReqDTO;
import com.yixin.hospital.entity.dto.req.drugType.DrugTypeEditReqDTO;
import com.yixin.hospital.entity.vo.DrugTypeVO;
import com.yixin.hospital.service.DrugTypeService;
import com.yixin.hospital.mapper.DrugTypeMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
* @author Administrator
* @description 针对表【drug_type(药品类别表)】的数据库操作Service实现
* @createDate 2024-04-03 17:16:38
*/
@Service
public class DrugTypeServiceImpl extends ServiceImpl<DrugTypeMapper, DrugType>
    implements DrugTypeService{

    @Override
    public ApiResult<List<Tree<Integer>>> treeList() {
        List<DrugType> drugTypeList = this.list();
        List<DrugTypeVO> deptVOList = BeanUtil.copyToList(drugTypeList, DrugTypeVO.class);
        TreeNodeConfig nodeConfig = new TreeNodeConfig();
        List<Tree<Integer>> treeList = TreeUtil.build(deptVOList, 0, nodeConfig,
                (node, tree) -> {
                    tree.setId(node.getId().intValue());
                    tree.setParentId(node.getPid().intValue());
                    tree.setName(node.getName());
                    tree.putExtra("abb", node.getAbb());
                    tree.putExtra("description", node.getDescription());
                    tree.putExtra("level", node.getLevel());
                    tree.putExtra("sort", node.getSort());
                });
        for (Tree<Integer> integerTree : treeList) {
            getChildren(integerTree);
        }
        treeList.sort(Comparator.comparing(o -> Integer.valueOf((int) o.get("sort"))));
        return ApiResult.success(treeList);
    }

    @Override
    public ApiResult add(DrugTypeAddReqDTO drugTypeAddReqDTO, String userName) {
        DrugType drugType = new DrugType();
        BeanUtil.copyProperties(drugTypeAddReqDTO, drugType);
        drugType.setPid(drugTypeAddReqDTO.getParentId());
        drugType.setCreatedBy(userName);
        drugType.setUpdatedBy(userName);
        this.save(drugType);
        return ApiResult.success(drugType.getId());
    }

    @Override
    public ApiResult edit(DrugTypeEditReqDTO drugTypeEditReqDTO, String userName) {
        DrugType drugType = new DrugType();
        BeanUtil.copyProperties(drugTypeEditReqDTO, drugType);
        drugType.setUpdatedBy(userName);
        this.updateById(drugType);
        return ApiResult.success();
    }

    private List<Tree<Integer>> getChildren(Tree<Integer> integerTree) {
        String abbStr = (String) integerTree.get(("abb"));
        List<String> abbArr = new ArrayList<>();
        for (int i = 0; i < abbStr.length(); i++) {
            abbArr.add(String.valueOf(abbStr.charAt(i)));
        }
        integerTree.put("abb", abbArr);
        if (!CollectionUtil.isEmpty(integerTree.getChildren())) {
            integerTree.getChildren().sort(Comparator.comparing(o -> Integer.valueOf((int) o.get("sort"))));
            for (Tree<Integer> child : integerTree.getChildren()) {
                List<String> childAbbArr = new ArrayList<>();
                childAbbArr.addAll(abbArr);
                childAbbArr.add((String)child.get("abb"));
                child.put("abb", String.join("", childAbbArr));
                getChildren(child);
            }
        }
        return null;
    }


}




